package cn.edu.bit.lixin.blog.action;

import java.util.List;

import cn.edu.bit.lixin.blog.biz.BlogManager;
import cn.edu.bit.lixin.blog.biz.ReplyManager;
import cn.edu.bit.lixin.blog.entity.Blog;
import cn.edu.bit.lixin.blog.entity.Reply;
import cn.edu.bit.lixin.blog.util.PageBean;

public class BlogAction extends BaseActionSupport {
	private BlogManager blogManager;
	private ReplyManager replyManager;
	private String title;
	private String blogContent;
	
	
	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public String getBlogContent() {
		return blogContent;
	}


	public void setBlogContent(String blogContent) {
		this.blogContent = blogContent;
	}


	public ReplyManager getReplyManager() {
		return replyManager;
	}


	public void setReplyManager(ReplyManager replyManager) {
		this.replyManager = replyManager;
	}


	public BlogManager getBlogManager() {
		return blogManager;
	}


	public void setBlogManager(BlogManager blogManager) {
		this.blogManager = blogManager;
	}

	/**
	 * 在首页显示经过分页的日志
	 * @return
	 */
	public String getPagedBlogs(){
		int currentPage = this.getParameterInt("page");//从URL中取出当前页
		int totalBlogs = blogManager.getTotalBlogs();//从数据库中查询出所有的日志数目
		
		//pageBean的一些初始化工作
		PageBean pageBean = new PageBean();
		pageBean.setCurrentPage(currentPage);
		pageBean.setResultCount(totalBlogs);
		pageBean.setPageCount(blogManager.PAGE_SIZE);
		pageBean.setPageUrl("index.jsp");
		
		//通过pageBean得到上一页和下一页
		pageBean.caculate();
		String jumpLinkHtml = pageBean.getPageJumpLinkHtml();
		
		List<Blog> blogs = blogManager.getPagedBlogs(currentPage);
		this.setAttribute("blogs", blogs);
		this.setAttribute("totalBlogs", totalBlogs);
		this.setAttribute("jumpLinkHtml", jumpLinkHtml);
		
		return SUCCESS;
	}
	
	/**
	 * 首页上点击某个日志的题目后，通过这个action取得这篇日志的详细信息
	 * 这些信息包括日志的名称，正文等等
	 * 同样也包括依附于这篇日志的所有回复
	 * 并将这些信息存入attribute，用于在jsp页面上进行提取
	 * @return
	 */
	public String getSpecificBlog(){
		int blogid = this.getParameterInt("blog");
		
		//查询这篇日志的所有信息并存储
		Blog blog = blogManager.getSpecificBlog(blogid);
		//this.setSession("blog", blog);
		this.setAttribute("blog", blog);
		
		//查询这篇日志的所有回复并存储
		List<Reply> replies = null;
		try{
			replies = replyManager.getSpecificReplies(blogid);
		}catch(Exception e){
			e.printStackTrace();
		}
		//this.setSession("replies", replies);
		this.setAttribute("replies", replies);
		
		return SUCCESS;
	}
	
	
	/**
	 * 根据前端传递过来的数据构造一个新的日志，并保存在数据库中
	 * @return
	 */
	public String addBlog(){
		if (blogManager.addBlog(title, blogContent)){
			System.out.println("-----------success add blog------------");
			return SUCCESS;
		}
		return INPUT;
	}
	
	/**
	 * 得到分页的Blog标题，显示在管理日志界面
	 * 当管理员单击某个Blog标题后，就可以修改这篇日志
	 * @return
	 */
	public String manageBlog(){
		int currentPage = this.getParameterInt("page");//从URL中取出当前页
		int totalBlogs = blogManager.getTotalBlogs();//从数据库中查询出所有的日志数目
		
		//pageBean的一些初始化工作
		PageBean pageBean = new PageBean();
		pageBean.setCurrentPage(currentPage);
		pageBean.setResultCount(totalBlogs);
		pageBean.setPageCount(blogManager.PAGE_SIZE);
		pageBean.setPageUrl("/blog/admin/manageBlog");
		
		//通过pageBean得到上一页和下一页
		pageBean.caculate();
		String jumpLinkHtml = pageBean.getPageJumpLinkHtml();
		
		List<Blog> blogs = blogManager.getPagedBlogs(currentPage);
		this.setAttribute("blogs", blogs);
		this.setAttribute("totalBlogs", totalBlogs);
		this.setAttribute("jumpLinkHtml", jumpLinkHtml);
		
		return SUCCESS;
	}
	
	/**
	 * 在管理日志页面单击了这篇日志的标题之后，就触发这个action
	 * 主要作用是传递关于这篇日志的信息到修改页面
	 * @return
	 */
	public String editBlog() {
		int blogid = this.getParameterInt("blog");

		// 查询这篇日志的所有信息并存储
		Blog blog = blogManager.getSpecificBlog(blogid);
		// this.setSession("blog", blog);
		
		//去掉最后的两个字符，可能是由于tinyMCE编辑器存储的原理引起的小bug，每篇文章的最后都会加上">这两个字符，去掉即可
		blog.setContent(blog.getContent().substring(0, blog.getContent().length() - 2));
		this.setAttribute("blog", blog);
		//System.out.println(blog.getContent());
		return SUCCESS;
	}
	
	/**
	 * 将修改过的日志提交到数据库保存
	 * @return
	 */
	public String modifyBlog(){
		int blogid = this.getParameterInt("blog");
		
		if (blogManager.updateBlog(blogid, title, blogContent)){
			return SUCCESS;
		}
		
		return INPUT;
	}
	
	/**
	 * 删除日志
	 * @return
	 */
	public String deleteBlog(){
		int blogid = this.getParameterInt("blog");
		
		if (blogManager.deleteBlog(blogid)){
			return SUCCESS;
		}
		
		return INPUT;
	}
	
	
	public String manageReply(){
		int blogid = this.getParameterInt("blog");
		
		//查询这篇日志的所有回复并存储
		List<Reply> replies = null;
		try{
			replies = replyManager.getSpecificReplies(blogid);
		}catch(Exception e){
			e.printStackTrace();
		}
		//this.setSession("replies", replies);
		this.setAttribute("replies", replies);
		
		return SUCCESS;
	}
}
